package com.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;

import javax.sql.DataSource;

/**
 * 配置QueryRunner和DataSource
 */
public class JdbcConfig {
    //注入基本类型和String类型  使用@Value 和 springEl表达式
    @Value("${jdbc.driverClassName}")
    private String driverClassName;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    //创建Bean属性
    @Bean(name = "runner")
    //多例
    @Scope("prototype")
    public QueryRunner creatQueryRunner(DataSource ds){
        return new QueryRunner(ds);
    }
    //创建bean属性
    @Bean(name = "ds")
    public DataSource getDataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(driverClassName);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        System.out.println(dataSource);
        return dataSource;
    }
}
